﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Profari_Heldur_Utan_Um_Prof
{
    public partial class InformationBefore : Form
    {
        #region SQL connection
        private static SqlConnection con = new SqlConnection("Data Source=orninn.com;Initial Catalog=Notendaprofanir;Persist Security Info=True;User ID=SonjaNew;Password=Léttjógúrt");
        private static DataTable dt = new DataTable();
        private static SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Project", con);
        private static DataSet ds = new DataSet();
        SqlDataReader r = null;
        #endregion

        #region static variables
        public static int id;
        public static string txtNameValue;
        public static string txtSsnValue;
        public static int? txtMobileValue;
        public static string txtAddressValue;
        public static bool? boolGenderMale;
        public static bool? boolGenderFemale;
        public static DateTime? txtBirthYearValue;
        public static string txtOtherValue;
        #endregion

        #region Variables
        int countHelpPressed = 1;        
        int beginLocationLabelX = 27;
        int beginLocationLabelY = 46;

        int beginLocationTextX = 145;
        int beginLocationTextY = 46;
        #endregion

        #region CheckBoxVariables
        bool name;
        bool ssn;
        bool mobile;
        bool address;
        bool gender;
        bool birthyear;
        bool other;
        bool userFeedback;
        #endregion

        public InformationBefore()
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();
        }

        private void InformationBefore_Load(object sender, EventArgs e)
        {
            lblHelpText.Hide();            
            getCheckBoxInfo();           
            //txtBoxHider();            
            sortTextBoxes();
            //hideAllTextBoxes();
        }

        //Controls what keys users are allowed to use in a textbox
        #region Key press
        private void txtSsn_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }

        private void txtMobile_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }
        #endregion

        #region Functions
        //Hides the textboxes when the form is loaded
        public void hideAllTextBoxes()
        {
            lblName.Hide();
            lblSsn.Hide();
            lblMobile.Hide();
            lblAddress.Hide();
            lblGender.Hide();
            lblBirthYear.Hide();
            lblOther.Hide();

            txtName.Hide();
            txtSsn.Hide();
            txtMobile.Hide();
            txtAddress.Hide();
            radFemale.Hide();
            radMale.Hide();
            txtBirthYear.Hide();
            txtOther.Hide();
        }
        
        private void txtBoxHider()
        {
            if (!name)
            {
                txtName.Hide();
                lblName.Hide();
            }
            if (!ssn)
            {
                txtSsn.Hide();
                lblSsn.Hide();
            }
            if (!mobile)
            {
                txtMobile.Hide();
                lblMobile.Hide();
            }
            if (!address)
            {
                txtAddress.Hide();
                lblAddress.Hide();
            }
            if (!gender)
            {
                radFemale.Hide();
                radMale.Hide();
                lblGender.Hide();
            }
            if (!birthyear)
            {
                txtBirthYear.Hide();
                lblBirthYear.Hide();
            }
            if (!other)
            {
                txtOther.Hide();
                lblOther.Hide();
            }
        }
        
        //The following function controls which textboxes and labels are showed.
        //What textbox is shown depends on which checkbox is checked under 
        //"Information from the user before the evaluation" in the CheckBoxes form.
        /*
         * For example if Name and Address are checked in the CheckBoxes form the textox and label
         * for the Name should come first and the textbox and label for the Address should come in the
         * next location on the form.
         */
        public void sortTextBoxes()
        {   
            
            if (!name)
            {
                txtName.Hide();
                lblName.Hide();
                txtSsn.Location = new Point(beginLocationTextX, beginLocationTextY);
                lblSsn.Location = new Point(beginLocationLabelX, beginLocationLabelY);
            }
            if (!ssn)
            {
                txtSsn.Hide();
                lblSsn.Hide();
            }
            if (!name && !ssn)
            {
                txtMobile.Location = new Point(beginLocationTextX, beginLocationTextY);
                lblMobile.Location = new Point(beginLocationLabelX, beginLocationLabelY);
            }
            if ((name && !ssn) || (!name && ssn))
            {
                txtMobile.Location = new Point(beginLocationTextX, beginLocationTextY + (40 * 1));
                lblMobile.Location = new Point(beginLocationLabelX, beginLocationLabelY + (40 * 1));
            }

            if (!mobile)
            {
                txtMobile.Hide();
                lblMobile.Hide();
            }

            if (!name && !ssn && !mobile)
            {
                txtAddress.Location = new Point(beginLocationTextX, beginLocationTextY);
                lblAddress.Location = new Point(beginLocationLabelX, beginLocationLabelY);
            }
            if ((name && !ssn && !mobile) || (!name && ssn && !mobile || !name && !ssn && mobile))
            {
                txtAddress.Location = new Point(beginLocationTextX, beginLocationTextY + (40 * 1));
                lblAddress.Location = new Point(beginLocationLabelX, beginLocationLabelY + (40 * 1));
            }
            if ((!name && ssn && mobile) || (name && !ssn && mobile) || (name && ssn && !mobile))
            {
                txtAddress.Location = new Point(beginLocationTextX, beginLocationTextY + (40 * 2));
                lblAddress.Location = new Point(beginLocationLabelX, beginLocationLabelY + (40 * 2));
            }

            if (!address)
            {
                txtAddress.Hide();
                lblAddress.Hide();
                panel1.Location = txtAddress.Location;
                lblGender.Location = lblAddress.Location;
            }

            if (!name && !ssn && !mobile && !address)
            {
                panel1.Location = new Point(beginLocationTextX, beginLocationTextY + (40 * 0));
                lblGender.Location = new Point(beginLocationLabelX, beginLocationLabelY + (40 * 0));
            }
            if ((!name && !ssn && !mobile && address)
                || (!name && !ssn && mobile && !address)
                || (!name && ssn && !mobile && !address)
                || (name && !ssn && !mobile && !address))
            {
                panel1.Location = new Point(beginLocationTextX, beginLocationTextY + (40 * 1));
                lblGender.Location = new Point(beginLocationLabelX, beginLocationLabelY + (40 * 1));
            }
            if ((!name && !ssn && mobile && address)
                || (!name && ssn && !mobile && address)
                || (!name && ssn && mobile && !address)
                || (name && !ssn && !mobile && address)
                || (name && !ssn && mobile && !address)
                || (name && ssn && !mobile && !address))
            {
                panel1.Location = new Point(beginLocationTextX, beginLocationTextY + (40 * 2));
                lblGender.Location = new Point(beginLocationLabelX, beginLocationLabelY + (40 * 2));
            }

            if ((!name && ssn && mobile && address)
                || (name && !ssn && mobile && address)
                || (name && ssn && !mobile && address)
                || (name && ssn && mobile && !address))
            {
                panel1.Location = new Point(beginLocationTextX, beginLocationTextY + (40 * 3));
                lblGender.Location = new Point(beginLocationLabelX, beginLocationLabelY + (40 * 3));
            }

            if (!gender)
            {
                radFemale.Hide();
                radMale.Hide();
                lblGender.Hide();
                panel1.Hide();
                txtBirthYear.Location = panel1.Location;
                lblBirthYear.Location = lblGender.Location;
            }

            if((name && ssn && mobile && address && !gender)
                || (name && ssn && mobile && !address && gender)
                || (name && ssn && !mobile && address && gender)
                || (name && !ssn && mobile && address && gender)
                || (!name && ssn && mobile && address && gender))
            {
                txtBirthYear.Location = new Point(beginLocationTextX, beginLocationTextY + (40 * 4));
                lblBirthYear.Location = new Point(beginLocationLabelX, beginLocationLabelY + (40 * 4));
            }

            if ((name && ssn && mobile && !address && !gender)
                || (name && ssn && !mobile && address && !gender)
                || (name && ssn && !mobile && !address && gender)
                || (name && !ssn && mobile && address && !gender)
                || (name && !ssn && mobile && !address && gender)
                || (name && !ssn && !mobile && address && gender)
                || (!name && ssn && mobile && address && !gender)
                || (!name && ssn && mobile && !address && gender)
                || (!name && ssn && !mobile && address && gender)
                || (!name && !ssn && mobile && address && gender))
            {
                txtBirthYear.Location = new Point(beginLocationTextX, beginLocationTextY + (40 * 3));
                lblBirthYear.Location = new Point(beginLocationLabelX, beginLocationLabelY + (40 * 3));
            }

            if ((name && ssn && !mobile && !address && !gender)
                || (name && !ssn && mobile && !address && !gender)
                || (name && !ssn && !mobile && address && !gender)
                || (name && !ssn && !mobile && !address && gender)
                || (!name && ssn && mobile && !address && !gender)
                || (!name && ssn && !mobile && address && !gender)
                || (!name && ssn && !mobile && !address && gender)
                || (!name && !ssn && mobile && address && !gender)
                || (!name && !ssn && mobile && !address && gender)
                || (!name && !ssn && !mobile && address && gender))
            {
                txtBirthYear.Location = new Point(beginLocationTextX, beginLocationTextY + (40 * 2));
                lblBirthYear.Location = new Point(beginLocationLabelX, beginLocationLabelY + (40 * 2));
            }

            if ((name && !ssn && !mobile && !address && !gender)
                || (!name && ssn && !mobile && !address && !gender)
                || (!name && !ssn && mobile && !address && !gender)
                || (!name && !ssn && !mobile && address && !gender)
                || (!name && !ssn && !mobile && !address && gender))
            {
                txtBirthYear.Location = new Point(beginLocationTextX, beginLocationTextY + (40 * 1));
                lblBirthYear.Location = new Point(beginLocationLabelX, beginLocationLabelY + (40 * 1));
            }

            if (!name && !ssn && !mobile && !address && !gender)
            {
                txtBirthYear.Location = new Point(beginLocationTextX, beginLocationTextY + (40 * 0));
                lblBirthYear.Location = new Point(beginLocationLabelX, beginLocationLabelY + (40 * 0));
            }

            if (!birthyear)
            {
                txtBirthYear.Hide();
                lblBirthYear.Hide();
                txtOther.Location = txtBirthYear.Location;
                lblOther.Location = lblBirthYear.Location;
            }

            if ((name && ssn && mobile && address && gender && !birthyear)
                || (name && ssn && mobile && address && !gender && birthyear)
                || (name && ssn && mobile && !address && gender && birthyear)
                || (name && ssn && !mobile && address && gender && birthyear)
                || (name && !ssn && mobile && address && gender && birthyear)
                || (!name && ssn && mobile && address && gender && birthyear))
            {
                txtOther.Location = new Point(beginLocationTextX, beginLocationTextY + (40 * 5));
                lblOther.Location = new Point(beginLocationLabelX, beginLocationLabelY + (40 * 5));
            }
            if ((name && ssn && mobile && address && !gender && !birthyear)
                || (name && ssn && mobile && !address && gender && !birthyear)
                || (name && ssn && mobile && !address && !gender && birthyear)
                || (name && ssn && !mobile && address && gender && !birthyear)
                || (name && ssn && !mobile && address && !gender && birthyear)
                || (name && ssn && !mobile && !address && gender && birthyear)
                || (name && !ssn && mobile && address && gender && !birthyear)
                || (name && !ssn && mobile && address && !gender && birthyear)
                || (name && !ssn && mobile && !address && gender && birthyear)
                || (name && !ssn && !mobile && address && gender && birthyear)
                || (!name && ssn && mobile && address && gender && !birthyear)
                || (!name && ssn && mobile && address && !gender && birthyear)
                || (!name && ssn && mobile && !address && gender && birthyear)
                || (!name && ssn && !mobile && address && gender && birthyear)
                || (!name && !ssn && mobile && address && gender && birthyear))
            {
                txtOther.Location = new Point(beginLocationTextX, beginLocationTextY + (40 * 4));
                lblOther.Location = new Point(beginLocationLabelX, beginLocationLabelY + (40 * 4));
            }

            if ((name && ssn && mobile && !address && !gender && !birthyear)
                || (name && ssn && !mobile && address && !gender && !birthyear)
                || (name && ssn && !mobile && !address && gender && !birthyear)
                || (name && ssn && !mobile && !address && !gender && birthyear)
                || (name && !ssn && mobile && address && !gender && !birthyear)
                || (name && !ssn && mobile && !address && gender && !birthyear)
                || (name && !ssn && mobile && !address && !gender && birthyear)
                || (name && !ssn && !mobile && address && gender && !birthyear)
                || (name && !ssn && !mobile && address && !gender && birthyear)
                || (name && !ssn && !mobile && !address && gender && birthyear)
                || (!name && ssn && mobile && address && !gender && !birthyear)
                || (!name && ssn && mobile && !address && gender && !birthyear)
                || (!name && ssn && mobile && !address && !gender && birthyear)
                || (!name && ssn && !mobile && address && gender && !birthyear)
                || (!name && ssn && !mobile && address && !gender && birthyear)
                || (!name && ssn && !mobile && !address && gender && birthyear)
                || (!name && !ssn && mobile && address && gender && !birthyear)
                || (!name && !ssn && mobile && address && !gender && birthyear)
                || (!name && !ssn && mobile && !address && gender && birthyear)
                || (!name && !ssn && !mobile && address && gender && birthyear))
            {
                txtOther.Location = new Point(beginLocationTextX, beginLocationTextY + (40 * 3));
                lblOther.Location = new Point(beginLocationLabelX, beginLocationLabelY + (40 * 3));
            }

            if ((name && ssn && !mobile && !address && !gender && !birthyear)
                || (name && !ssn && mobile && !address && !gender && !birthyear)
                || (name && !ssn && !mobile && address && !gender && !birthyear)
                || (name && !ssn && !mobile && !address && gender && !birthyear)
                || (name && !ssn && !mobile && !address && !gender && birthyear)
                || (!name && ssn && mobile && !address && !gender && !birthyear)
                || (!name && ssn && !mobile && address && !gender && !birthyear)
                || (!name && ssn && !mobile && !address && gender && !birthyear)
                || (!name && ssn && !mobile && !address && !gender && birthyear)
                || (!name && !ssn && mobile && address && !gender && !birthyear)
                || (!name && !ssn && mobile && !address && gender && !birthyear)
                || (!name && !ssn && mobile && !address && !gender && birthyear)
                || (!name && !ssn && !mobile && address && gender && !birthyear)
                || (!name && !ssn && !mobile && address && !gender && birthyear)
                || (!name && !ssn && !mobile && !address && gender && birthyear))
            {
                txtOther.Location = new Point(beginLocationTextX, beginLocationTextY + (40 * 2));
                lblOther.Location = new Point(beginLocationLabelX, beginLocationLabelY + (40 * 2));
            }

            if ((name && !ssn && !mobile && !address && !gender && !birthyear)
                || (!name && ssn && !mobile && !address && !gender && !birthyear)
                || (!name && !ssn && mobile && !address && !gender && !birthyear)
                || (!name && !ssn && !mobile && address && !gender && !birthyear)
                || (!name && !ssn && !mobile && !address && gender && !birthyear)
                || (!name && !ssn && !mobile && !address && !gender && birthyear))
            {
                txtOther.Location = new Point(beginLocationTextX, beginLocationTextY + (40 * 1));
                lblOther.Location = new Point(beginLocationLabelX, beginLocationLabelY + (40 * 1));
            }

            if (!name && !ssn && !mobile && !address && !gender && !birthyear)
            {
                txtOther.Location = new Point(beginLocationTextX, beginLocationTextY + (40 * 0));
                lblOther.Location = new Point(beginLocationLabelX, beginLocationLabelY + (40 * 0));
            }

            if (!other)
            {
                txtOther.Hide();
                lblOther.Hide();
            }
        }

        public void setUserInfo()
        {
            //getMaxID();
            if (!name)
                txtNameValue = "User";
            else
                txtNameValue = txtName.Text;

            if (!ssn)
                txtSsnValue = null;
            else
                txtSsnValue = txtSsn.Text;

            if (!mobile)
                txtMobileValue = null;
            else
                txtMobileValue = int.Parse(txtMobile.Text);

            if (!address)
                txtAddressValue = null;
            else
                txtAddressValue = txtAddress.Text;

            if (!gender)
            {
                boolGenderMale = false;
                boolGenderFemale = false;
            }
            else
            {
                if (radFemale.Checked == true)
                {
                    boolGenderFemale = true;
                    boolGenderMale = false;
                }
                else
                {
                    boolGenderFemale = false;
                    boolGenderMale = true;
                }
            }

            if (!birthyear)
                txtBirthYearValue = null;
            else
                txtBirthYearValue = DateTime.Parse(txtBirthYear.Text);

            if (!other)
                txtOtherValue = null;
            else
                txtOtherValue = txtOther.Text;
        }
        #endregion

        #region Buttons
        private void btnNext_Click(object sender, EventArgs e)
        {
            insertUserInfo();
            System.Threading.Thread introTextTask = new System.Threading.Thread(new System.Threading.ThreadStart(OpenStartTask));
            introTextTask.SetApartmentState(System.Threading.ApartmentState.STA);
            introTextTask.Start();
            this.Close();
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            //If user wants to quit the project he is moved to the first window
            DialogResult cancelProject = MessageBox.Show("Are you sure you want to quit the evaluation?", "Quit", MessageBoxButtons.YesNo);
            if (cancelProject == DialogResult.Yes)
            {
                this.Close();
            }
        }        
        
        private void btnHelp_Click(object sender, EventArgs e)
        {
            if (countHelpPressed % 2 == 1)
            {
                lblHelpText.Show();
                countHelpPressed++;
            }
            else if (countHelpPressed % 2 == 0)
            {
                lblHelpText.Hide();
                countHelpPressed++;
            }
        }
        #endregion

        #region SQL Command
        private void insertUserInfo()
        {
            //setUserInfo();
            getMaxID();
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO UserInfo(name ,ssn ,mobile ,usersAddress ,female ,male ,birthD ,other ) VALUES(@name, @ssn, @mobile, @userAddress, @female, @male, @birthD, @other)", con);
                cmd.Parameters.AddWithValue("@name", txtName.Text == "" ? "User "+id : (object)txtName.Text);
                cmd.Parameters.AddWithValue("@ssn", txtSsn.Text == "" ? DBNull.Value : (object)txtSsn.Text);
                cmd.Parameters.AddWithValue("@mobile", txtMobile.Text == "" ? DBNull.Value : (object)txtMobile.Text);
                cmd.Parameters.AddWithValue("@userAddress", txtAddress.Text == "" ? DBNull.Value : (object)txtAddress.Text);
                if (gender)
                {
                    cmd.Parameters.AddWithValue("@female", (object)radFemale.Checked);
                    cmd.Parameters.AddWithValue("@male", (object)radMale.Checked);
                }
                else
                {
                    cmd.Parameters.AddWithValue("@female", DBNull.Value);
                    cmd.Parameters.AddWithValue("@male", DBNull.Value);
                }
                cmd.Parameters.AddWithValue("@birthD", txtBirthYear.Text == "" ? DBNull.Value : (object)txtBirthYear.Text);
                cmd.Parameters.AddWithValue("@other", txtOther.Text == "" ? DBNull.Value : (object)txtOther.Text);
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());
            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
        }

        private void getMaxID()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT TOP 1 id FROM UserInfo ORDER BY id desc" , con);
                r = cmd.ExecuteReader();
                while (r.Read())
                {
                    id = r.GetInt32(0);
                    id++;
                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());
            }
            finally
            {
                if (con != null)
                    con.Close();
            }
        }

        private void getCheckBoxInfo()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from checkBoxInfo where groupsID="+StartChooseProject.globalgroupsID + " and evaluationID="+StartChooseProject.globalevaluationID, con);
                r = cmd.ExecuteReader();
                while (r.Read())
                {
                    name = r.GetBoolean(4);
                    ssn = r.GetBoolean(5);
                    mobile = r.GetBoolean(6);
                    address = r.GetBoolean(7);
                    gender = r.GetBoolean(8);
                    birthyear = r.GetBoolean(9);
                    other = r.GetBoolean(10);
                    userFeedback = r.GetBoolean(11);
                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());
            }
            finally
            {
                if (con != null)
                    con.Close();
            }
        }

        #endregion

        #region OpenForm
        public void OpenStartTask()
        {
            Application.Run(new StartTask());
        }
        #endregion

    }
}
